package bmptest.bmp.service.SysUser;

import bmptest.bmp.entity.SysUser;
import bmptest.bmp.mapper.sysUser.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

@Service
public class SysUserService {

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    public RedisTemplate<String,String> redisTemplate;

    String s;
    String create_by_id;
    String create_time;
    String create_by_name;
    String password = "8dea5faaa2b98a0634570d4de36482f0";
    String brightpassword = "123456";

    public Integer addSysUser(SysUser sysUser){
        return sysUserMapper.addSysUser(sysUser);
    }

    private String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    //从session获取当前登录用户id
    private String getSessionUserid(HttpSession session){
        String user_id = (String)session.getAttribute("user_id");
        return user_id;
    }

    //从session获取当前登录用户名称
    private String getSessionUsername(HttpSession session){
        String user_name = (String)session.getAttribute("user_name");
        return user_name;
    }

    //获取对当前时间
    private String getCurrentDate(){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        String currentDate = formatter.format(date);
        return currentDate;
    }

    @Transactional
    public String addAll(SysUser sysUser){
        String isvalidate = "1";
        String validateusername = sysUser.getUser_name();
        String validatephone = sysUser.getPhone();
        String validateemail = sysUser.getEmail();
        //正则校验手机号
        Pattern patternphone = Pattern.compile("^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\\d{8}$");
        Boolean isvalidatephone = patternphone.matcher(validatephone).matches();
        //正则校验邮箱
        Pattern patternemail = Pattern.compile("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?.)+[a-z]{2,}$");
        Boolean isvalidateemail = patternemail.matcher(validateemail).matches();
        if(validateusername.length()>8||validateusername.length()<2){
            isvalidate = "0";
            return isvalidate;
        }else if(!isvalidatephone){
            isvalidate = "0";
            return isvalidate;
        }else if(!isvalidateemail){
            isvalidate = "0";
            return isvalidate;
        }else{
            s = this.getUUID();
            if(s!=null&&!s.equals("")){
                sysUser.setUser_id(s);
            }
            redisTemplate.setKeySerializer(RedisSerializer.string());
            create_by_id = redisTemplate.opsForValue().get("uid");
            if(create_by_id!=null&&!create_by_id.equals("")){
                sysUser.setCreate_by_id(create_by_id);
            }
            create_by_name = redisTemplate.opsForValue().get("uname");
            if(create_by_name!=null&&!create_by_name.equals("")){
                sysUser.setCreate_by_name(create_by_name);
            }
            create_time=this.getCurrentDate();
            if(create_time!=null&&!create_time.equals("")){
                sysUser.setCreate_time(create_time);
            }
            sysUser.setPassword(password);
            sysUser.setBrightpassword(brightpassword);
            sysUser.setStatus(1);
            this.addSysUser(sysUser);
            return isvalidate;
        }
    }

    public void deleteUser(String user_id){
        sysUserMapper.deleteUser(user_id);
    }

    public String updateUser(SysUser sysUser){
        String isvalidate = "1";
        String validateusername = sysUser.getUser_name();
        String validatephone = sysUser.getPhone();
        String validateemail = sysUser.getEmail();
        //正则校验手机号
        Pattern patternphone = Pattern.compile("^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\\d{8}$");
        Boolean isvalidatephone = patternphone.matcher(validatephone).matches();
        //正则校验邮箱
        Pattern patternemail = Pattern.compile("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?.)+[a-z]{2,}$");
        Boolean isvalidateemail = patternemail.matcher(validateemail).matches();
        if(validateusername.length()>8||validateusername.length()<2){
            isvalidate = "0";
            return isvalidate;
        }else if(!isvalidatephone){
            isvalidate = "0";
            return isvalidate;
        }else if(!isvalidateemail){
            isvalidate = "0";
            return isvalidate;
        }else{
            sysUserMapper.updateUser(sysUser);
            return isvalidate;
        }
    }

    public void updateStatus(SysUser sysUser){
        sysUserMapper.updateStatus(sysUser);
    }

    public List<SysUser> searchUser(String  username,Integer status,String begindate,String enddate){
        String newbegindate = begindate.replaceAll("-","");
        String newenddate = enddate.replaceAll("-","");
        BigDecimal newbegindatewithoutsymbol;
        BigDecimal newenddatewithoutsymbol;
        if(newenddate!=null&&!newenddate.equals("")){
            newbegindatewithoutsymbol =new BigDecimal(newbegindate);
        }else{
            newbegindatewithoutsymbol = new BigDecimal("0");
        }
        if(newenddate!=null&&!newenddate.equals("")){
            newenddatewithoutsymbol =new BigDecimal(newenddate);
        }else{
            newenddatewithoutsymbol = new BigDecimal("99999999999999");
        }
        List<SysUser> allsysuser ;
        List<SysUser> rightdatesysuser = new LinkedList<>();
        String symbol = "%";
        String user_name = symbol+username+symbol;
        if(!"null".equals(String.valueOf(status))){
            allsysuser = sysUserMapper.searchUser(user_name,status);
        }else{
            allsysuser = sysUserMapper.searchUserU(user_name);
        }
        for(int i=0;i<allsysuser.size();i++){
            SysUser sysUser = allsysuser.get(i);
            String createtimewithoutsymbol = sysUser.getCreate_time().replaceAll("-","");
            BigDecimal decimalcreatetimewithoutsymbol =new BigDecimal(createtimewithoutsymbol);
            if(decimalcreatetimewithoutsymbol.compareTo(newbegindatewithoutsymbol)>=0&&decimalcreatetimewithoutsymbol.compareTo(newenddatewithoutsymbol)<=0){
                rightdatesysuser.add(sysUser);
            }
        }
        return rightdatesysuser;
    }

    public String queryRoleIdByUserId(String userid){
        String roleid = sysUserMapper.queryRoleIdByUserId(userid);
        return roleid;
    }

}
